var base_url="http://"+location.host+"/";
console.log(base_url);
$(".swiper-container").swiper({
    loop: true,
    autoplay: 2000
});

$("#searchInput").blur(function(e){
    var key=$(this).val();
    location.href="/search?key="+key;

});

var like_url=base_url+"api/like";
//0-13之间产生一个随机数
var like_index=parseInt(Math.random()*13);

function change_like(index){
    $.ajax({
        url:like_url,
        type:"get",
        success:function(res){
            html="";
            for(let i=9*index;i<9*index+9;i++){
                html+='<a href="javascript:;" data_book_id="'+res.data[i].book_id+'" data_intro="'+res.data[i].intro+'" class="weui-grid js_grid book_detail"><div class=""><img width="100%" src="'+res.data[i].cover+'" alt=""></div><p class="weui-grid__label"><b>书名:</b>'+res.data[i].book_name+'</p><p class="weui-grid__label"><b>作者:</b>'+res.data[i].author_name+'</p></a>';
            }
            $(".like_list").html(html);
            $(".book_detail").click(function(e){
                detail_url=base_url+"detail/"+$(this).attr("data_book_id");
                document.cookie="img="+$(this).find("img").attr("src")+";path=/";
                document.cookie="intro="+$(this).attr("data_intro")+";path=/";
                parent.location.href=detail_url
            });
        }
    });
}

//获取畅销书
function get_hot(){
    hot_url=base_url+"api/categrey/1";
    $.ajax({
        url:hot_url,
        type:"get",
        success:function(res){
            console.log(res);
        }
    });
}

change_like(like_index);
get_hot();

$(".like_change").click(function(e){
    var index=parseInt(Math.random()*13);
    while(index==like_index){
        index=parseInt(Math.random()*13);
    }
    change_like(index);
});

